<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/22 15:16
 * Remark:
 */

namespace App\Http\Middleware\Company;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\RequestConsts\RequestMethodConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Models\Company\CompanyModel;
use Closure;
use Illuminate\Http\Exceptions\HttpResponseException;

class CompanyLegitimacyMiddleware
{
    private CompanyModel $_companyModel;

    public function __construct()
    {
        $this->_companyModel = new CompanyModel();
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $method = strtolower($request->method());
        switch ($method){
            case RequestMethodConsts::GET_METHOD:
                //
                break;
            case RequestMethodConsts::POST_METHOD:
                // 新增的时候校验名称是否重复
                $this->verifyCompanyNameIsExists($request->post('company_name'));
                // 新增时候校验他的父公司是否存在
                if($request->post('company_parent_id')){
                    $this->verifyCompanyIsExists($request->post('company_parent_id'),"父公司不存在或已删除");
                }
                break;
            case RequestMethodConsts::PUT_METHOD:
                $companyId = $request->companyId;
                // 修改的时候校验公司是否存在
                $this->verifyCompanyIsExists($companyId,"当前公司不存在");
                // 如果修改了公司名称 判断当前公司名称是否已经存在
                if($request->post('company_name')){
                    $this->verifyCompanyNameIsExistsByEdit($companyId, $request->post('company_name'));
                }
                // 如果修改了父节点 判断当前节点是否有子节点如果有则不能修改
                if($request->post('company_parent_id')) {
                    if($request->post('company_parent_id') == $companyId){
                        throw new HttpResponseException( ResponseFunc::returnJson(
                            'error',
                            '',
                            "上级公司不能为自己",
                            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                        ));
                    }
                    $this->verifyCompanyParentIsExists($request->post('company_parent_id'),"当前公司存在子公司不能调整架构");
                }
                break;
            case RequestMethodConsts::PATCH_METHOD:
                //批量删除判断
                if($request->post('company_ids')){
                    $companyIds = $request->post('company_ids');
                    foreach($companyIds as $companyId){
                        // 删除的时候校验公司是否存在
                        $this->verifyCompanyIsExists($companyId,$companyId."公司不存在");
                        // 删除的时候校验当前节点是否存在子节点
                        $this->verifyCompanyParentIsExists($companyId,$companyId."公司存在子公司不能删除");
                    }
                }

                if(isset($request->companyId) && $request->companyId){
                    // 删除的时候校验公司是否存在
                    $this->verifyCompanyIsExists($request->companyId,"当前公司不存在");
                    // 删除的时候校验当前节点是否存在子节点
                    $this->verifyCompanyParentIsExists($request->companyId,"当前公司存在子公司不能删除");
                }
                break;
            default:
                break;
        }

        return $next($request);
    }

    /**
     * 校验公司名称是否存在
     * @param $companyName
     */
    private function verifyCompanyNameIsExists($companyName){
        $isExist = $this->_companyModel->isExistByCondition([
            'company_name' => $companyName,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '公司名已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                
            ));
        }
    }

    /**
     * 修改时检验公司名称是否存在
     * @param $companyId
     * @param $companyName
     */
    private function verifyCompanyNameIsExistsByEdit($companyId, $companyName){
        $editIds = $this->_companyModel->setGetEditCheckerIds('company_id',$companyId);
        $editNames = $this->_companyModel->setGetEditCheckerNames('company_name',$companyName);
        $isExists = $this->_companyModel->editNameChecker($editNames,$editIds);
        if($isExists){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '当前公司名已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }


    /**
     * 校验公司是否存在
     * @param $companyId
     * @param $msg
     */
    private function verifyCompanyIsExists($companyId,$msg){
        $isExist = $this->_companyModel->isExistByCondition([
            'company_id' => $companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                $msg,
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                
            ));
        }
    }

    /**
     * 校验当前公司是否为它公司父级
     * @param $companyId
     * @param $msg
     */
    private function verifyCompanyParentIsExists($companyId,$msg){
        $isExist = $this->_companyModel->isExistByCondition([
            'company_parent_id' => $companyId,
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                $msg,
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }
}